/**
 * 
 */
package utility.tools;

import gui.trainer.TrainerFrame;
import handwritten.character.CharacterCollection;
import handwritten.character.CharacterCollectionManager;
import handwritten.character.RawHandWrittenCharacter;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Enumeration;

import javax.swing.SwingUtilities;

import recognition.input.CellInput;

import utility.io.HWCharacterIO;
import utility.io.HWCharacterIOFactory;
import auth.User;

/**
 * @author lreymondin
 *
 */
public class InputDBCreator {
	public static void main(String args[]) throws IOException{
		HWCharacterIO io = HWCharacterIOFactory.getInstance().getHWCharacterIO("XML");
		try {
			io.loadDataBase();
		} catch (IOException e) {
			System.out.print("oupps mla programmer io exeption");
			e.printStackTrace();
		}
		
		SwingUtilities.invokeLater(new Runnable(){
			public void run(){
				TrainerFrame.getInstance();
			}
		});
		
		try {
			Thread.sleep(10000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		System.out.println("Collection loaded");
		for(User user : CharacterCollectionManager.getInstance().getUsers()){
			ArrayList<CharacterCollection<?>> cols = CharacterCollectionManager.getInstance().getCollections(user);
			for(CharacterCollection<?> col : cols){
				System.out.println("Type : "+col.getType().getCanonicalName());
				Enumeration<String> enume = col.keys();
				while(enume.hasMoreElements()){
					String id = enume.nextElement();
					System.out.println(id+" "+col.size(id));
				}
			}
		}
		
		User user = CharacterCollectionManager.getInstance().getUsers().iterator().next();
		

		@SuppressWarnings("unchecked") //Should be safe is the Manager is well done
		CharacterCollection<RawHandWrittenCharacter> collection = (CharacterCollection<RawHandWrittenCharacter>) CharacterCollectionManager.getInstance().getCollection(user,"RAW",RawHandWrittenCharacter.class); 
		
		File dir = new File("outputs/CSV/");
		dir.mkdirs();
		
		PrintWriter inputsWriter = new PrintWriter(new File("outputs/CSV/inputs.csv"));
		PrintWriter outputsWriter = new PrintWriter(new File("outputs/CSV/outputs.csv"));
		
		
		
		Enumeration<String> keys = collection.keys();
		outputsWriter.print(keys.nextElement());
		while(keys.hasMoreElements()){
			outputsWriter.print(","+keys.nextElement());
		}
		outputsWriter.println();
		keys = collection.keys();
		
		int length = collection.size();
		String id;
		CellInput creator = new CellInput();
		double[] input;
		int count = 0;
		while(keys.hasMoreElements()){
			id = keys.nextElement();
			for(int i = 0; i<collection.size(id); i++){
				input = creator.createInput((collection.get(id, i).getGesture())).getInput();
				writeInput(input, inputsWriter);
				writeOutputs(count,length,outputsWriter);
			}
			count++;    
		}
		outputsWriter.flush();
		outputsWriter.close();
		inputsWriter.flush();
		inputsWriter.close();
		System.out.println("done...");
	}
	
	public static void writeInput(double[] input, PrintWriter writer){
		for(int i = 0; i<input.length;i++)
			writer.print(input[i]+((i!=input.length-1)?",":""));
		writer.println();
	}
	
	public static void writeOutputs(int id, int length, PrintWriter writer){
		for(int i = 0; i<length; i++)
			writer.print((i==id?1:0)+((i!=length-1)?",":""));
		writer.println();
	}
}
